#include "net_manager.h"

void SoftbusDemo(void)
{
    printf("app_SoftbusToolStart in\r\n");
    SelectWifiOrEthernetInit();
    ToolsImportLocalCredential();
    printf("******SoftbusDemoStart ******\r\n");
}

APP_FEATURE_INIT(SoftbusDemo);

void ToolsImportLocalCredential(void)
{
    // Use a fixed verification code
    char strBuf[DA_GROUPID_LEN_MAX] = {0};
    char weightsBuf[DA_FAST_AUTH_WEIGHT_LEN_MAX+1] = {0};
    AuthInfo authInfoBuf = {0};
    authInfoBuf.type = FASTAUTH;
    GetInputString("Please input auth code(32 characters):", strBuf, DA_GROUPID_LEN_MAX);
    GetInputString("Please input weight(4 characters):", weightsBuf, DA_FAST_AUTH_WEIGHT_LEN_MAX+1);
    authInfoBuf.data.accData.userId = GetInputNumber("Please input userId(userId >= 0):");
    strncpy(authInfoBuf.data.accData.authCode, strBuf, strlen(strBuf));
    if (strlen(weightsBuf) == 0) {
        strncpy(weightsBuf, DEFAULT_WEIGHTS, strlen(DEFAULT_WEIGHTS));
    }
    int ret = AddAuthInfo(authInfoBuf);
    if(ret == 0){
        printf("ToolsImportCredential setting seccuss\r\n");
    }else{
        printf("ToolsImportCredential setting failed\r\n");
        return;
    }
    char userIdStr[USERID_LEN_MAX+1] = {0};
    sprintf(userIdStr, "%d", authInfoBuf.data.accData.userId);
    ret = KH_SaveAuthCodeWeights(authInfoBuf.data.accData.authCode, weightsBuf, userIdStr);
    if (ret != 0) {
        printf("KH_SaveAuthCodeWeights error, ret = %d\r\n", ret);
    }
}

void ToolsDeleteLocalCredential(void)
{
    int32_t clearmode = 0;
    clearmode = GetInputNumber("Please input role type, eg:0 - ALL_CLEAR, 1 - ASSIGN_TYPE :");
    if(clearmode != 0 && clearmode != 1){
        printf("ToolsDeleteLocalCredential setting clearmode failed\r\n");
        return;
    }

    char strBuf[DA_GROUPID_LEN_MAX] = {0};
    AuthInfo authInfoBuf = {0};
    authInfoBuf.type = FASTAUTH;

    if(clearmode == 1){
        GetInputString("Please input auth code(32 characters):", strBuf, DA_GROUPID_LEN_MAX);
        strncpy(authInfoBuf.data.accData.authCode, strBuf, DA_GROUPID_LEN_MAX);
    }

    int ret = DeleteAuthInfo(authInfoBuf, clearmode);
    if(ret == 0){
        printf("ToolsDeleteLocalCredential setting seccuss\r\n");
    }else{
        printf("ToolsDeleteLocalCredential setting failed\r\n");
    }
}

void SelectWifiOrEthernetInit(void)
{
    int ret = 0;
    GetCurrentInitConfig();

    printf("Please input 1 to select the wifi init\r\n");
    printf("Please input 2 to select the ethernet init\r\n");
    int cmd = GetInputNumber("Please input the selection:");
    if (cmd == 1) {
        ret = SetInitConfig(WIFI_INIT_CONFIG);
        if (ret < 0){
            printf("UtilsSetValue error ret=%d\r\n", ret);
        } else {
            printf("Next initialization is wifi, which takes effect after reboot\r\n");
        }
    } else if (cmd == 2) {
        ret = SetInitConfig(ETHERNET_INIT_CONFIG);
        if (ret < 0){
            printf("UtilsSetValue error ret=%d\r\n", ret);
        } else {
            printf("Next initialization is ethernet, which takes effect after reboot\r\n");
        }
    } else {
        printf("The cmd is invalid\r\n");
    }
}